'OHRRPGCE CUSTOM - Original Main module
'(C) Copyright 1997-2005 James Paige and Hamster Republic Productions
'Monkey Port by Landon Ritchie

Import mojo
Import fantomEngine
Import customeng
Import editorui
Import spriteset

Global g:game

Class game Extends App
	Field eng:engine
	Field tm:ftObject
	Field uif:ftFont
	Field mainui:EditorUI
	
	Method OnCreate:Int()
		Local c:Int

		SetUpdateRate(60)
		eng = New engine
		'Set the canvas size of a usual Android canvas 480x800
		eng.SetCanvasSize(640,480)
		uif = eng.LoadFont("arial16.txt")
		'Create Blank Spritesets
		Local ss:SpriteSet = New SpriteSet
		ss.CreateBlankSet(eng)
		eng.SSets.AddLast(ss)
		
		
		mainui = New EditorUI
		mainui.Create(uif,eng)
		
		'Load the tile map created by Tiled
		'tm = eng.CreateTileMap("maps/sewer.json", 12, 12 )
		'Set its scale factor 
		'tm.SetScale(1.6)
		'Set the scale mod factor for each tile of the map
		'tm.SetTileSModXY(0.05,0.05)
		Return 0
	End
	'------------------------------------------
	Method OnUpdate:Int()
		Local d:Float = Float(eng.CalcDeltaTime())/60.0
		If eng.GetPause() = False Then
			mainui.Update()
			eng.Update(Float(d))
			
			'Remove a tile when you click left with the mouse
			If MouseHit( MOUSE_LEFT ) Then
				'tm.SetTileIDAt(eng.GetTouchX(),eng.GetTouchY(),-1)
			Endif
			'Set a random tile
			If MouseHit( MOUSE_RIGHT ) Then
				'tm.SetTileIDAt(eng.GetTouchX(),eng.GetTouchY(),Rnd(0,15))
			Endif
			'Move the camera with the cursor keys
			'If KeyDown(KEY_LEFT) Then eng.SetCamX(-2,True)
			'If KeyDown(KEY_RIGHT) Then eng.SetCamX(2,True)
			'If KeyDown(KEY_UP) Then eng.SetCamY(-2,True)
			'If KeyDown(KEY_DOWN) Then eng.SetCamY(2,True)
		Endif
		Return 0
	End
	'------------------------------------------
	Method OnRender:Int()
		Local x:Int = eng.GetTouchX()
		Local y:Int = eng.GetTouchY()
		Cls 0,0,0
		mainui.RenderSplash()
		eng.Render()
		eng.PickPixel()
		'Print some debugging messages liek FPS and the tile ID under the mouse cursor
		'DrawText("FPS:"+eng.GetFPS(),310,10)
		'DrawText(x+":"+y+"="+tm.GetTileIDAt(x,y),310,40)
		Return 0
	End
	'------------------------------------------
	Method OnResume:Int()
		eng.SetPause(False)
		SetUpdateRate(60)
		Return 0
	End
	'------------------------------------------
	Method OnSuspend:Int()
		eng.SetPause(True)
		SetUpdateRate(5)
		Return 0
	End



End



Function Main:Int()
	g = New game
	Return 0
End